<template>
  <a-modal
    title="单位管理"
    :width="850"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :destroyOnClose="true"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :span="14">
            <!-- 唯一编号 -->
            <a-row>
              <a-col :span="24">
                <a-form-item
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  label="字典分类"
                >
                  <DictSelect
                    dictCategory="dictCategory"
                    :showCode="true"
                    :dictCategoryOpt="true"
                    v-decorator="['dictCategory', {rules: [{ required: false, initialValue: '' }]}]"
                  >
                  </DictSelect>
                  <!-- <a-input :disabled="editModel" v-decorator="['idno', {rules: [{ required: true, message: '编号不能为空' }]}]" /> -->
                </a-form-item>
              </a-col>
            </a-row>
            <!-- 代码  -->
            <a-row :gutter="24">
              <a-col :span="24">
                <a-form-item
                  label="代码"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                >
                  <a-input v-decorator="['dictCode', {rules: [{ required: true, message: '代码不能为空' }]}]" />
                </a-form-item>
              </a-col>
            </a-row>
            <!-- 词条 -->
            <a-row :gutter="24">
              <a-col :span="24">
                <a-form-item
                  label="词条"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                >
                  <a-input v-decorator="['dictEntry', {rules: [{ required: true, message: '词条不能为空' }]}]" />
                </a-form-item>
              </a-col>
            </a-row>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getDataInfoByPid, saveInfo } from '../dict'
import DictSelect from '@/views/sys/modules/DictSelect'

export default {
  name: 'DictModal',
  components: {
    DictSelect
  },
  data () {
    return {
      // form: this.$form.createForm(this),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      dictInfo: {
        sysSeq: 0,
        pid: ''
      },
      visible: false
    }
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  created () {
  },
  methods: {
    showAddModal (dictCategory) {
      this.visible = true
      if (dictCategory === '字典分类') {
        dictCategory = 'dictCategory'
      }
      this.form.getFieldDecorator('dictCategory', { initialValue: dictCategory, rules: [{ required: true }] })
    },
    showEditModal (pid) {
      if (pid) {
        this.dictInfo.pid = pid
        this.confirmLoading = true
        this.form.setFieldsValue({ })
        getDataInfoByPid(pid, {})
          .then(res => {
            if (res && res.code && res.code === 'C1' && res.data) {
              this.dictInfo.sysSeq = res.data.sysSeq
              this.dictInfo.pid = res.data.pid
              this.form.setFieldsValue(res.data)
              // console.log(this.form.getFieldsValue())
            }
            // console.log(this.orgInfo.sysSeq)
          })
        this.confirmLoading = false
      }
      this.visible = true
    },
    close () {
      this.$emit('ok')
      this.visible = false
    },
    handleOk () {
      const _this = this
      // 触发表单验证
      this.form.validateFields((err) => {
        // 验证表单没错误
        if (!err) {
          _this.confirmLoading = true
          // 模拟后端请求 2000 毫秒延迟
          new Promise((resolve) => {
            resolve(saveInfo(Object.assign(this.dictInfo, this.form.getFieldsValue()), {}))
          }).then(() => {
            // Do something
            _this.$message.success('保存成功')
            _this.$emit('ok')
          }).catch(() => {
            // Do something
          }).finally(() => {
            _this.confirmLoading = false
            _this.close()
            _this.dictInfo = {
              sysSeq: 0,
              pid: '' // 主键
            }
          })
        }
      })
    },
    handleCancel () {
      this.visible = false
    }

  }
}
</script>
